// SkinProgressCtrl.cpp : implementation file
//

#include "stdafx.h"
#include "SkinProgressCtrl.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CSkinProgressCtrl

IMPLEMENT_DYNAMIC(CSkinProgressCtrl, CButton)

CSkinProgressCtrl::CSkinProgressCtrl()
: m_pImgZero(m_pBitmaps[0])
, m_pImgFull(m_pBitmaps[1])
, m_nPos(0)
{
	ZeroMemory (m_pBitmaps, sizeof(m_pBitmaps));

}

CSkinProgressCtrl::~CSkinProgressCtrl()
{
	for (int i = 0; i < sizeof(m_pBitmaps) / sizeof(m_pBitmaps[0]); i++) {
		delete m_pBitmaps[i];
		m_pBitmaps[i] = NULL;
	}
}


BEGIN_MESSAGE_MAP(CSkinProgressCtrl, CStatic)
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
END_MESSAGE_MAP()



// CSkinProgressCtrl message handlers



void CSkinProgressCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{

	// TODO:  Add your code to draw the specified item
	//CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
	//pDC->ExcludeClipRect(&lpDrawItemStruct->rcItem);

	UpdateSkin();
}

BOOL CSkinProgressCtrl::CreateInternal( const RECT& rect, CWnd* pParentWnd, UINT nID )
{
	CRect rt(rect);
	ASSERT(m_pImgZero);
	rt.right = rt.left + m_pImgZero->GetWidth();
	rt.bottom = rt.top + m_pImgZero->GetHeight();

	if (!CButton::Create(m_strCommandName, BS_OWNERDRAW| WS_VISIBLE, rt, pParentWnd, nID)) {
		return FALSE;
	}

	return m_ctrlTooltip.Create(this);
}

//========================================================================
BOOL CSkinProgressCtrl::LoadSkinImage( LPCTSTR lpszFilename )
{
	Gdiplus::Bitmap bmControl(lpszFilename);
	const INT cxImg = bmControl.GetWidth();
	const INT cyImg = bmControl.GetHeight();
	if (cxImg < 1 && cyImg < 1)
		return FALSE;

	const INT cyCtrl = cyImg / 2;
	INT yPos = 0;
	VERIFY (m_pImgZero = bmControl.Clone(0, yPos, cxImg, cyCtrl, PixelFormatDontCare));
	VERIFY (m_pImgFull = bmControl.Clone(0, yPos += cyCtrl, cxImg, cyCtrl, PixelFormatDontCare));

	return TRUE;
}

void CSkinProgressCtrl::DrawIt( Gdiplus::Bitmap* pbmWnd, const RECT& rect )
{
	ASSERT(pbmWnd);
	ASSERT(m_pImgZero);

	const int cxRect = (rect.right - rect.left);
	const int cyRect = rect.bottom - rect.top;
	SetWindowPos(NULL,
		rect.left - m_cxBorder,
		rect.top - m_cyBorder,
		cxRect,	cyRect,
		SWP_NOREDRAW);

	Gdiplus::Graphics graphic(pbmWnd);
	graphic.DrawImage(m_pImgZero, rect.left, rect.top, m_pImgZero->GetWidth(), m_pImgZero->GetHeight()); // background bitmap

	const int w = ((m_pImgFull->GetWidth()) * m_nPos) / 100;
	const int h = m_pImgFull->GetHeight(); //rect.bottom - rect.top;
	if (w > 0 && h > 0){
		Gdiplus::Rect destinationRect(rect.left, rect.top, w, h);
		graphic.DrawImage(m_pImgFull, destinationRect, 0, 0, w, h, Gdiplus::UnitPixel);
	}

}

void CSkinProgressCtrl::SetPos( INT val )
{
	if(val != m_nPos){
		m_nPos = min(100, max(0, val));
		Invalidate();
	}
}


void CSkinProgressCtrl::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default

	__super::OnMouseMove(nFlags, point);

	if (GetCapture() == this){
		CRect r;
		GetClientRect(&r);

		int nPos = (point.x * 100) / r.Width();
		SetPos(nPos);
	}
}

BOOL CSkinProgressCtrl::SetTooltip( LPCTSTR lpszText )
{
	m_ctrlTooltip.AddTool(this, lpszText);
	m_ctrlTooltip.UpdateTipText(lpszText, this);
	m_ctrlTooltip.Activate(TRUE);

	return TRUE;
}
BOOL CSkinProgressCtrl::PreTranslateMessage(MSG* pMsg)
{
	// TODO: Add your specialized code here and/or call the base class
	m_ctrlTooltip.RelayEvent(pMsg);

	return __super::PreTranslateMessage(pMsg);
}

void CSkinProgressCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	SetCapture();

	__super::OnLButtonDown(nFlags, point);
}

void CSkinProgressCtrl::OnLButtonUp(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	ReleaseCapture();

	__super::OnLButtonUp(nFlags, point);
}

CRect CSkinProgressCtrl::GetPlacement( const INT cxFrame, const INT cyFrame )
{
	CRect rt = CSkinControl::GetPlacement(cxFrame, cyFrame);
	switch (m_Align) {
	case TopLeft:
	case TopRight:
	case BottomLeft:
	case BottomRight:
		rt.right = rt.left + m_pImgZero->GetWidth();	// don't change size
		rt.bottom = rt.top + m_pImgZero->GetHeight();
		break;
	case TopCenter:
	case BottomCenter:
		rt.right = rt.left + (cxFrame - 2 * m_Offset.x);	// stretch horizon
		rt.bottom = rt.top + m_pImgZero->GetHeight();
		break;
	default:
		ASSERT(FALSE);
	}

	return rt;
}